package com.grandbroad.eim.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.easy.west.westclient.api.vodcommen.bean.Commen;
import com.grandbroad.eim.R;
import com.grandbroad.eim.entry.WhcatBean;
import com.grandbroad.eim.utils.CacheUtils;
import com.grandbroad.eim.utils.TimeUtils;
import com.orhanobut.hawk.Hawk;

import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 评论列表适配器
 */
public class VideoCommenAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Commen> commenList = new ArrayList<>();
    private int NORMAL=21;
    private int EMPTY=22;
    private boolean empty =false;

    public void setEmpty(boolean empty) {
        this.empty = empty;
    }

    public void setCommenList(List<Commen> commenList) {
        this.commenList = commenList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = null;
        if (getItemViewType(i)==NORMAL)
        {
             view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.commen_item, viewGroup, false);
            return new CommenViewHolder(view);
        }
        else
        {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.empty_view,viewGroup,false);
            return new EmptyViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof CommenViewHolder)
        {
            CommenViewHolder commenViewHolder = (CommenViewHolder) viewHolder;
            Commen commen = commenList.get(i);
            if (commen.getOwner().getAvatar()!=null)
            {
                x.image().bind(commenViewHolder.imgCommenAvatar,commen.getOwner().getAvatar().toString());
            }
            else
            {
                //看看是不是自己
                if (commen.getOwner().getId()==CacheUtils.getUser().getId())
                {
                    //看看能否使用微信头像
                    WhcatBean whcatBean = Hawk.get("wechat");
                    if (whcatBean!=null)
                    {
                        x.image().bind(commenViewHolder.imgCommenAvatar,whcatBean.getHeadimgurl());
                    }
                    else
                    {
                        commenViewHolder.imgCommenAvatar.setBackgroundResource(R.drawable.icon_my_head_default);
                    }
                }
                else
                {
                    commenViewHolder.imgCommenAvatar.setBackgroundResource(R.drawable.icon_my_head_default);
                }
            }
            commenViewHolder.tvCommenTime.setText(TimeUtils.formatGTtime(commen.getCreated_at()));
            commenViewHolder.tvCommenUsername.setText(commen.getOwner().getName());
            commenViewHolder.tvCommenContent.setText(commen.getContent());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (empty)
        {
            return EMPTY;
        }
        else
        {
            return NORMAL;
        }
    }

    @Override
    public int getItemCount()
    {
        return commenList.size();
    }

    class CommenViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_commen_avatar)
        ImageView imgCommenAvatar;
        @BindView(R.id.tv_commen_username)
        TextView tvCommenUsername;
        @BindView(R.id.tv_commen_time)
        TextView tvCommenTime;
        @BindView(R.id.tv_commen_content)
        TextView tvCommenContent;
        public CommenViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    class EmptyViewHolder extends RecyclerView.ViewHolder
    {

        public EmptyViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
